package com.blt.car.me;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blt.car.R;
import com.blt.car.base.BaseTitleBarActivity;
import com.blt.car.base.ViewHolder;

import java.util.ArrayList;

import blt.app_core.api.base.CoreEvent;
import blt.app_core.api.base.CoreManager;
import blt.app_core.api.invoce.InvoiceApi;
import blt.app_core.log.Logger;

/**
 * Created by xgp on 2017/6/20.
 */

public class ApplyBillActivity extends BaseTitleBarActivity {
    public static final String TRAVEL_LIST = "travel_list";
    public static final String TRAVEL_ID = "travel_id";
    public static final String INVOICE_AMOUNT = "INVOICE_AMOUNT";


    private static final int E_BILL = 101;
    private static final int TISSUE_BILL = 202;
    private int billType;
    private ArrayList<Integer> travelIds;
    private int amount;


    private LinearLayout llPaper, llEmail;
    private Button bt_paper_bill, bt_email_bill;
    private EditText etCompany;
    private EditText etBillSpecification;
    private EditText etReceiveName;
    private EditText etReceiveTel;

    private EditText etEmail;
    private EditText etAddress;
    private EditText etTisEmail;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_apply_bill;
    }

    @Override
    protected void initData() {
        super.initData();
        Bundle b = getIntent().getExtras().getBundle(TRAVEL_ID);
        if (b != null) {
            travelIds = b.getIntegerArrayList(TRAVEL_LIST);
            amount = b.getInt(INVOICE_AMOUNT);
        }
    }

    @Override
    protected void initView(ViewHolder holder) {
        mTitleBar.setTitle("行程发票");
        TextView tvInvoiceAmount = holder.get(R.id.apply_bill_amount);
        tvInvoiceAmount.setText(amount + "元");
        llPaper = holder.get(R.id.ll_paper);
        llEmail = holder.get(R.id.ll_email);
        holder.get(R.id.btn_apply_bill);
        etCompany = holder.get(R.id.bill_apply_company);
        etReceiveName = holder.get(R.id.bill_apply_receiver_name);
        etReceiveTel = holder.get(R.id.bill_apply_receiver_tel);
        etEmail = holder.get(R.id.bill_apply_email);
        etBillSpecification = holder.get(R.id.bill_apply_specification);
        etAddress = holder.get(R.id.bill_apply_address);
        etTisEmail = holder.get(R.id.bill_apply_tis_email);
        bt_paper_bill = holder.get(R.id.bt_paper_bill);
        bt_email_bill = holder.get(R.id.bt_email_bill);


        holder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                applyInvoice();
            }
        }, R.id.btn_apply_bill);
    }

    private void applyInvoice() {
        String company = etCompany.getText().toString();
        String content = etBillSpecification.getText().toString();
        String title = "高速过路费";
        StringBuilder sp = new StringBuilder();
        for (Integer id : travelIds) {
            sp.append(id);
            sp.append(",");
        }
        sp.replace(sp.length() - 1, sp.length(), "");
        String travelId = sp.toString();
        Logger.d(travelId);
        if (billType == E_BILL) {
            String email = etEmail.getText().toString();
            if (TextUtils.isEmpty(email)) {
                toastShort("邮箱不能为空");
                return;
            }

            CoreManager.getCore(InvoiceApi.class).applyInvoice(travelId, "0", company, title, amount, content, email, null, null, null);

        } else {
            String address = etAddress.getText().toString();
            String tisEmail = etTisEmail.getText().toString();
            String receiver = etReceiveName.getText().toString();
            String tel = etReceiveTel.getText().toString();
            if (TextUtils.isEmpty(address)) {
                toastShort("请输入详细的地址");
                return;
            }
            if (TextUtils.isEmpty(tisEmail)) {
                toastShort("请填写邮编");
            }
            CoreManager.getCore(InvoiceApi.class).applyInvoice(travelId, "1", company, title,
                    amount, content, tisEmail, receiver, tel, address);

        }

    }

    @CoreEvent(coreClientClass = InvoiceApi.class)
    public void onApplySuccess() {
        toastShort("提交成功");
        openActivity(BillManagerActivity.class);
    }

    public void emailBill(View view) {
        billType = E_BILL;
        llPaper.setVisibility(View.GONE);
        llEmail.setVisibility(View.VISIBLE);
        bt_paper_bill.setBackgroundResource(R.drawable.bg_shape_gray);
        bt_paper_bill.setTextColor(getResources().getColor(R.color.text_color_gray));
        bt_email_bill.setBackgroundResource(R.drawable.bg_shape_modify);
        bt_email_bill.setTextColor(getResources().getColor(R.color.text_color_orange));
    }

    public void paperBill(View view) {
        billType = TISSUE_BILL;
        llPaper.setVisibility(View.VISIBLE);
        llEmail.setVisibility(View.GONE);
        bt_email_bill.setBackgroundResource(R.drawable.bg_shape_gray);
        bt_email_bill.setTextColor(getResources().getColor(R.color.text_color_gray));
        bt_paper_bill.setBackgroundResource(R.drawable.bg_shape_modify);
        bt_paper_bill.setTextColor(getResources().getColor(R.color.text_color_orange));
    }


}
